import { NodeSize } from '../config'

export default function csvInput(flow) {
  flow.register('CsvInput', ({ PolygonNode, PolygonNodeModel, h }) => {
    class Node extends PolygonNode {
      getIconShape() {
        return h(
          'svg',
          {
            width: NodeSize,
            height: NodeSize,
            viewBox: '0 0 1024 1024'
          },
          h(
            'path',
            {
              fill: '#3D6480',
              d: 'M890.294857 795.672381v75.166476l-127.366095-127.390476-32.768 32.768 127.390476 127.390476-76.653714 0.024381v46.32381l155.721143-0.024381v-154.258286z'
            }
          ),
          h(
            'path',
            {
              fill: '#EFF9FE',
              d: 'M607.865905 318.244571V214.430476H281.673143v595.163429h280.746667l150.235428-150.235429-0.975238-341.113905h-103.814095z m-177.493334 285.988572H352.304762v-' +
                '19.529143h78.043428v19.529143z m212.138667-82.480762H352.304762v-19.504762h290.182095v19.504762z m0-82.456381H352.304762v-19.529143h290.182095v19.529143z'
            }
          ),
          h(
            'path',
            {
              fill: '#3D6480',
              d: 'M753.127619 318.488381l-41.667048-41.667048h-62.122666V214.698667l-41.71581-41.691429H240.225524v678.009905h280.771047l41.423239-' +
                '41.423238H281.673143V214.430476h326.192762v103.814095h103.814095l0.975238 341.113905 41.325714-41.350095z'
            }
          ),
          h(
            'path',
            {
              fill: '#3D6480',
              d: 'M352.304762 419.766857h290.182095v19.529143H352.304762zM352.304762 502.247619h290.182095v19.529143H352.304762zM352.304762 584.704h78.043428v19.529143H352.304762z'
            }
          )
        )
      }

      getLabelShape() {
        return h(
          'text',
          {
            fill: '#000000',
            fontSize: 12,
            x: NodeSize / 2,
            y: NodeSize + 20,
            textAnchor: 'middle',
            dominantBaseline: 'middle'
          },
          'CSV文件输入'
        )
      }

      getShape() {
        const { x, y, width, height } = this.getAttributes()
        const transform = `matrix(1 0 0 1 ${x - width / 2} ${y - height / 2})`
        return h(
          'g',
          {
            transform
          },
          [
            h(
              'rect',
              {
                rx: 5,
                ry: 5,
                width: NodeSize,
                height: NodeSize,
                fill: '#fff',
                stroke: '#3D6380',
                strokeWidth: 1
              }
            ),
            this.getIconShape(),
            this.getLabelShape()
          ]
        )
      }
    }
    class Model extends PolygonNodeModel {
      constructor(data, graphModel) {
        super(data, graphModel)
        this.points = [
          [NodeSize / 2, 0],
          [NodeSize, NodeSize / 2],
          [NodeSize / 2, NodeSize],
          [0, NodeSize / 2]
        ]
      }
    }

    return {
      view: Node,
      model: Model
    }
  })
}
